package javaweb.servlet.impl;

import javaweb.dao.BookDao;
import javaweb.dao.impl.BookDaoImpl;
import javaweb.pojo.Book;
import javaweb.pojo.Page;
import javaweb.servlet.BookServlet;

import java.util.List;

/**
 * @author shkstart
 * @creat -07-26 22:15
 */
public class BookServletImpl  implements BookServlet {
    BookDao bs= new BookDaoImpl();
    @Override
    public void addBook(Book book) {
        bs.addBook(book);
    }

    @Override
    public void detleteBookId(Integer id) {
bs.deleteBookId(id);
    }

    @Override
    public void upDateBook(Book book) {
bs.upDateBook(book);
    }

    @Override
    public Book queryBookId(Integer id) {
        return bs.queryBookId(id);
    }

    @Override
    public List<Book> qurtyBooks() {
        return bs.queryBooks();
    }
    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTocalCount = bs.queryForTocalCount();//设置总记录数
        page.setPageTocalCount(pageTocalCount);//设置总记录数
        Integer pageTocal=pageTocalCount/pageSize;
        if (pageTocalCount%pageSize>0){
            pageTocal+=1;
        }
        page.setPageTocal(pageTocal);
        page.setPageNo(pageNo);
        int begin=(page.getPageNo()-1)*pageSize;//当前页数据的索引
        List<Book> items=bs.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Book> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTocalCount = bs.queryForTocalCountByPrice(min,max);//设置总记录数
        page.setPageTocalCount(pageTocalCount);//设置总记录数
        Integer pageTocal=pageTocalCount/pageSize;
        if (pageTocalCount%pageSize>0){
            pageTocal+=1;
        }
        page.setPageTocal(pageTocal);
        page.setPageNo(pageNo);
        int begin=(page.getPageNo()-1)*pageSize;//当前页数据的索引
        List<Book> items=bs.queryForPageItemsByPrice(begin,pageSize,min,max);
        page.setItems(items);
        return page;
    }
}
